//
//  DetailsViewController.m
//  Cat
//
//  Created by lushitong on 2019/1/7.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "DetailsViewController.h"
#import "BalanceTableView.h"
#import "BrokTableView.h"
#import "WSDatePickerView.h"

@interface DetailsViewController ()<QKSegmentControlDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) QKSegmentControl *nextSegmentControl;
@property (nonatomic, strong) UIScrollView *nextScrollView;
@property (nonatomic, strong) BalanceTableView *balanceTableView;
@property (nonatomic, strong) BrokTableView *allBrokTableView;  // 全部
@property (nonatomic, strong) BrokTableView *payBrokTableView; // 已经付款
@property (nonatomic, strong) BrokTableView *suanBrokTableView; // 已经结算
@property (nonatomic, strong) BrokTableView *faileBrokTableView; // 已经失效

@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self.topTitleView setDetailDic:@{Nav_Title:@"我的订单",Nav_RightTxt:@"筛选"}.mutableCopy];
	[self loadAllSubViews];
}

- (void)p_topRightBtnClick {
	
	WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonth scrollToDate:nil CompleteBlock:^(NSDate *selectDate) {
		NSString *dateString = [selectDate stringWithFormat:@"yyyy-MM"];
		NSArray *array = [dateString componentsSeparatedByString:@"-"];
		NSLog(@"%@",array);
	}];
	
	datepicker.dateLabelColor = [UIColor colorWithHexString:@"000000"];//年-月-日-时-分 颜色
	datepicker.datePickerColor = [UIColor colorWithHexString:@"000000"];//滚轮日期颜色
	datepicker.doneButtonColor = [UIColor colorWithHexString:@"#F7AC01"];//确定按钮的颜色
	datepicker.isForeign = NO;
	datepicker.maxLimitDate = [NSDate date:nil WithFormat:@"yyyy-MM-dd HH:mm"];
	datepicker.minLimitDate = [NSDate date:nil WithFormat:@"yyyy-MM-dd HH:mm"];
	[datepicker show];
}

- (void)clickButton:(UIButton *)button segement:(QKSegmentControl *)segement {
	[self.nextScrollView setContentOffset:CGPointMake(ScreenWidth * button.tag, 0) animated:YES];
}

- (void)loadAllSubViews {
	[self.view addSubview:self.nextSegmentControl];
	[self.view addSubview:self.nextScrollView];
	
	[self.nextScrollView addSubviews:@[self.allBrokTableView,self.payBrokTableView,self.suanBrokTableView,self.faileBrokTableView]];
	self.nextSegmentControl.frame = CGRectMake(0.0, self.topTitleView.bottom, ScreenWidth, kQKFitModuleDecimal(30));
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	CGFloat contentOffsetX = scrollView.contentOffset.x;
	NSInteger currentIndex = (NSInteger)(contentOffsetX / ScreenWidth);
	[self.nextSegmentControl changeSelectedSegmentIndex:currentIndex anitmated:NO];
}

+ (NSArray *)getCurrentYearAndMonth:(NSInteger)afterYear {
	// 获取代表公历的NSCalendar对象
	NSCalendar *gregorian = [[NSCalendar alloc]
							 initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
	// 获取当前日期
	NSDate* dt = [NSDate date];
	// 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
	unsigned unitFlags = NSCalendarUnitYear |
	NSCalendarUnitMonth |  NSCalendarUnitDay |
	NSCalendarUnitHour |  NSCalendarUnitMinute |
	NSCalendarUnitSecond | NSCalendarUnitWeekday;
	// 获取不同时间字段的信息
	NSDateComponents* comp = [gregorian components: unitFlags
										  fromDate:dt];
	return @[@(comp.year + afterYear),@(comp.month)];
}

#pragma mark----- lazy init -----

- (QKSegmentControl *)nextSegmentControl {
	if (!_nextSegmentControl) {
		_nextSegmentControl = [[QKSegmentControl alloc] initWithFrame:CGRectZero withTitles:@[@"全部",@"已付款",@"已结算",@"已失效"] style:QKSegmentControlStytleBoldLine];
		_nextSegmentControl.backgroundColor = [UIColor whiteColor];
		_nextSegmentControl.selectedFontSize = kQKFitFont(14);
		_nextSegmentControl.fontSize = kQKFitFont(14);
		_nextSegmentControl.selectedColor = COLOR_WITH_HEX(0x57524F);
		_nextSegmentControl.normalColor = COLOR_WITH_HEX(0x57524F);
		_nextSegmentControl.bottomLineColor = COLOR_WITH_HEX(0x57524F);
		_nextSegmentControl.delegate = self;
	}
	return _nextSegmentControl;
}

- (UIScrollView *)nextScrollView {
	if (!_nextScrollView) {
		_nextScrollView =  [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.topTitleView.bottom + kQKFitModuleDecimal(30.0), ScreenWidth, ScreenHeight - self.topTitleView.height - kQKFitModule(1.0) - kQKFitModuleDecimal(30.0))];
		_nextScrollView.delegate = self;
		_nextScrollView.pagingEnabled = YES;
		_nextScrollView.showsVerticalScrollIndicator = NO;
		_nextScrollView.showsHorizontalScrollIndicator = NO;
		_nextScrollView.bounces = NO;
		[_nextScrollView setContentSize:CGSizeMake(self.nextSegmentControl.titles.count * ScreenWidth , 0)];
	}
	return _nextScrollView;
}

- (BalanceTableView *)balanceTableView {
	if (!_balanceTableView) {
		_balanceTableView  =[[BalanceTableView alloc] initWithFrame:CGRectMake(ScreenWidth, 0.0, ScreenWidth, ScreenHeight - self.topTitleView.height - kQKFitModule(1.0) - kQKFitModuleDecimal(30.0)) sections:1];
	}
	return _balanceTableView;
}

- (BrokTableView *)allBrokTableView {
	if (!_allBrokTableView) {
		_allBrokTableView = [[BrokTableView alloc] initWithFrame:CGRectMake(0.0, 0.0, ScreenWidth, self.nextScrollView.height) sections:1 withType:@""];
	}
	return _allBrokTableView;
}

- (BrokTableView *)payBrokTableView {
	if (!_payBrokTableView) {
		_payBrokTableView = [[BrokTableView alloc] initWithFrame:CGRectMake(ScreenWidth, 0.0, ScreenWidth, self.nextScrollView.height) sections:1 withType:@"1"];
	}
	return _payBrokTableView;
}

- (BrokTableView *)suanBrokTableView {
	if (!_suanBrokTableView) {
		_suanBrokTableView = [[BrokTableView alloc] initWithFrame:CGRectMake(2 * ScreenWidth, 0.0, ScreenWidth, self.nextScrollView.height) sections:1 withType:@"2"];
	}
	return _suanBrokTableView;
}

- (BrokTableView *)faileBrokTableView {
	if (!_faileBrokTableView) {
		_faileBrokTableView = [[BrokTableView alloc] initWithFrame:CGRectMake(3 * ScreenWidth, 0.0, ScreenWidth, self.nextScrollView.height) sections:1 withType:@"3"];
	}
	return _faileBrokTableView;
}

@end
